﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace SZSLSystem.DAL
{
	/// <summary>
	///数据访问类 PreferentialInfo (create by HC 2013/1/14 22:28:41)
	/// </summary>
	public class PreferentialInfo
	{
		public PreferentialInfo(){}
		public bool Exists(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(OID) from PreferentialInfo");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.Exists(strSql.ToString(), parameters);
		}

		public int Delete(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from PreferentialInfo");
			strSql.Append(" where OID=@OID  ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public int Add(SZSLSystem.Model.PreferentialInfo model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into PreferentialInfo( ");
			strSql.Append("[OID],[PreferentialType],[PreferentialTime],[PreferentialPeople],[PreferentialReason])");
			strSql.Append("values (@OID,@PreferentialType,@PreferentialTime,@PreferentialPeople,@PreferentialReason)");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@PreferentialType",SqlDbType.VarChar,50),
					new SqlParameter ("@PreferentialTime",SqlDbType.DateTime),
					new SqlParameter ("@PreferentialPeople",SqlDbType.VarChar,50),
					new SqlParameter ("@PreferentialReason",SqlDbType.VarChar,1000)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.PreferentialType;
			parameters[2].Value = model.PreferentialTime;
			parameters[3].Value = model.PreferentialPeople;
			parameters[4].Value = model.PreferentialReason;
			object obj = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
			if (obj == null)
			{
				return 1;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		public  SZSLSystem.Model.PreferentialInfo GetModel(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select top 1 [OID],[PreferentialType],[PreferentialTime],[PreferentialPeople],[PreferentialReason] from PreferentialInfo");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			SZSLSystem.Model.PreferentialInfo model=new SZSLSystem.Model.PreferentialInfo();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				DataRow row = ds.Tables[0].Rows[0];
						model.OID=row["OID"].ToString();
						model.PreferentialType=row["PreferentialType"].ToString();
					if (row["PreferentialTime"].ToString()!="")
					{
						model.PreferentialTime=DateTime.Parse(row["PreferentialTime"].ToString());
					}
						model.PreferentialPeople=row["PreferentialPeople"].ToString();
						model.PreferentialReason=row["PreferentialReason"].ToString();

				return model;
			}
			else
			{
				return null;
			}
		}

		public int Update(SZSLSystem.Model.PreferentialInfo model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update PreferentialInfo set ");
			strSql.Append("[OID]=@OID,");
			strSql.Append("[PreferentialType]=@PreferentialType,");
			strSql.Append("[PreferentialTime]=@PreferentialTime,");
			strSql.Append("[PreferentialPeople]=@PreferentialPeople,");
			strSql.Append("[PreferentialReason]=@PreferentialReason");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@PreferentialType",SqlDbType.VarChar,50),
					new SqlParameter ("@PreferentialTime",SqlDbType.DateTime),
					new SqlParameter ("@PreferentialPeople",SqlDbType.VarChar,50),
					new SqlParameter ("@PreferentialReason",SqlDbType.VarChar,1000)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.PreferentialType;
			parameters[2].Value = model.PreferentialTime;
			parameters[3].Value = model.PreferentialPeople;
			parameters[4].Value = model.PreferentialReason;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select [OID],[PreferentialType],[PreferentialTime],[PreferentialPeople],[PreferentialReason]");
			strSql.Append(" FROM PreferentialInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(int top,string strWhere,string fieldOrder)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select");
			if(top>0)
			{
				strSql.Append(" top "+top.ToString());
			}
			strSql.Append(" [OID],[PreferentialType],[PreferentialTime],[PreferentialPeople],[PreferentialReason]");
			strSql.Append(" FROM PreferentialInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
				strSql.Append(" order by "+fieldOrder);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(string showFields,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+showFields);
			strSql.Append(" FROM PreferentialInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public object GetSingle(string fieldName,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+fieldName);
			strSql.Append(" FROM PreferentialInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.GetSingle(strSql.ToString());
		}

	}
}